package com.wmx.main.text04;

import com.wmx.Dbutils.DbutilClass;

import java.lang.reflect.Field;

/**
 * @author:wmx 2025-03-29 13:06
 * 1、功能:
 * 2、参数{
 * <p>
 * <p>
 * }
 **/
public class SqlGenerate {
    static StringBuilder  stringBuilder = new StringBuilder();//拼接
    static boolean   flagClass = false;//判断类是否有注解
    static boolean flagClassField = false;//判断字段是否有注解
    static Field  name = null;//name字段
    static String className = null;//简单的类名


    public static String generateSelect(Class<?> clz) throws Exception {

        //当类有注解时
        boolean annotation = clz.isAnnotationPresent(Table.class);
        if(annotation){
            flagClass = true;
            //当字段有注解时
            name = clz.getDeclaredField("name");
            name.setAccessible(true);
            boolean annotationPresent = name.isAnnotationPresent(Column.class);

            if(annotationPresent){
                flagClassField = true;
            }
        }


       //无注解的情况：select id,name from user
        if(flagClass == false &&  flagClassField == false){
            stringBuilder.append("select ");
            className = clz.getSimpleName();//获取简单类名
            Field[] fields = clz.getDeclaredFields();//获取字段
            for (Field field : fields) {
                stringBuilder.append(field.getName()+",");
            }
            stringBuilder.delete(stringBuilder.length()-1,stringBuilder.length());
            stringBuilder.append(" from "+ className);
            System.out.println(stringBuilder.toString());


        }
        //字段和类上都有注解：select id,username from t_user
        else if(flagClass &&  flagClassField){



            String valueTable = (String) DbutilClass.getClassAnnotation(clz,Table.class);//注解表名
            String valueColumn = (String) DbutilClass.getFieldAnnotation(name,Column.class);//注解子段名

            stringBuilder.append("select id,"+valueColumn+" from " + valueTable);
            System.out.println(stringBuilder.toString());

        }
        //在字段上有注解：select id,name from user
        else if(flagClassField){
            className = clz.getSimpleName();//获取简单类名
            String valueColumn = (String) DbutilClass.getFieldAnnotation(name,Column.class);//注解子段名
            stringBuilder.append("select id,"+valueColumn+" from " + className);
            System.out.println(stringBuilder.toString());
        }
        //类上有注解：select id,name from t_user
        else if(flagClass){
            className = clz.getSimpleName();//获取简单类名
            String valueTable = (String) DbutilClass.getClassAnnotation(clz,Table.class);//注解表名
            Field[] fields = clz.getDeclaredFields();//获取字段
            stringBuilder.append("select ");
            for (Field field : fields) {
                stringBuilder.append(field.getName()+",");
            }
            stringBuilder.delete(stringBuilder.length()-1,stringBuilder.length());
            stringBuilder.append(" from " + valueTable);
            System.out.println(stringBuilder.toString());
        }





        return  null;
    }
}
